package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.mappers.MockQuestionMapper;
import com.tanhua.dubbo.mappers.OptionMapper;
import com.tanhua.model.domain.MockQuestion;
import com.tanhua.model.domain.Option;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class MockQuestionApiImpl implements MockQuestionApi {
    @Autowired
    private MockQuestionMapper mockQuestionMapper;
    @Autowired
    private OptionMapper optionMapper;
    /**
     * 查询
     * @return
     */
    @Override
    public List<MockQuestion> findMockQuestionListByMockId(String mockId) {
        LambdaQueryWrapper<MockQuestion> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(MockQuestion::getMockId,mockId);
        List<MockQuestion> mockQuestions = mockQuestionMapper.selectList(wrapper);//缺少option信息
        mockQuestions= mockQuestions.stream().map(
                m -> {
                    List<Option> options = optionMapper.selectOptionListByMockQuestionId(m.getId());
                    m.setOptions(options);
                    return m;
                }
        ).collect(Collectors.toList());

        return mockQuestions;
    }

    @Override
    public MockQuestion findQuestionById(String questionId) {
        LambdaQueryWrapper<MockQuestion> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(MockQuestion::getId,questionId);
        return mockQuestionMapper.selectOne(wrapper);
    }
}
